<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠菜单按钮 -->
        <div class="toggleMEnu" @click="isCollapse = !isCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          :unique-opened="true"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="defaultActive"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menusList" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsList[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="setDefaultActive('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <transition>
          <router-view></router-view>
        </transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menusList: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制菜单折叠
      isCollapse: false,
      defaultActive: ''
    }
  },
  created () {
    this.getMenusList()
    this.defaultActive = sessionStorage.getItem('activeIndex')
  },
  methods: {
    // 退出的事件函数
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取左侧菜单
    async getMenusList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menusList = res.data
      this.$message.success(res.meta.msg)
    },
    setDefaultActive (index) {
      this.defaultActive = index
      sessionStorage.setItem('activeIndex', index)
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
  .el-header {
    background: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .logo {
      display: flex;
      align-items: center;
      span {
        font-size: 24px;
        color: #ffffff;
        margin-left: 8px;
      }
    }
  }
  .el-aside {
    background: #373d41;
  }
  .el-main {
    background: #eaedf1;
  }
}
.toggleMEnu {
  width: 100%;
  height: 25px;
  font-size: 12px;
  line-height: 25px;
  text-align: center;
  color: #ffffff;
  background: #4a5064;
  letter-spacing: 8px;
  cursor: pointer;
  user-select: none;
  letter-spacing: 2px;
}
.el-menu {
  border-right: 0;
}
.iconfont {
  margin-right: 10px;
}
.v-enter,
.v-leave-to {
  transform: translate(10px, 0);
  opacity: 0;
}
.v-enter-active {
  transition: transform 0.8s;
}
.v-enter-to {
  transform: translate(0, 0);
  opacity: 1;
}
</style>
